//
//  MineInfoViewController.swift
//  MT
//
//  Created by 陈悦 on 2020/5/13.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory
import YPNavigationBarTransition
import RxSwift
import RxCocoa
import Moya
import Result

class MineInfoViewController: BaseViewController {
    /// 个人信息
    var model: MineInfoModel? {
        didSet {
            guard let model = model else { return }
            heightLab.attributedText = mc_add(text: "身高: ", UIColor.text.x9b9b9b, font: PingFangSCRegularFont(12), appendText: model.height != nil ? "\(model.height!)cm" : "-", UIColor.text.x1d1a1a, appendFont: PingFangSCMediumFont(12))
                //mc_addForegroundColor(text: model.height != nil ? "身高:\(model.height!)cm" : "身高:-", UIColor.text.x1d1a1a, range: NSRange.init(location: 3, length: 5))
            weightLab.attributedText = mc_add(text: "体重: ", UIColor.text.x9b9b9b, font: PingFangSCRegularFont(12), appendText: model.weight != nil ? "\(model.weight!)kg" : "-", UIColor.text.x1d1a1a, appendFont: PingFangSCMediumFont(12))
                //mc_addForegroundColor(text: model.weight != nil ? "体重:\(model.weight!)kg" : "体重:-", UIColor.text.x1d1a1a, range: NSRange.init(location: 3, length: 4))
            starLab.attributedText = mc_add(text: "星座: ", UIColor.text.x9b9b9b, font: PingFangSCRegularFont(12), appendText: model.constellation != nil ? "\(model.constellation!)" : "-", UIColor.text.x1d1a1a, appendFont: PingFangSCMediumFont(12))
                //mc_addForegroundColor(text: model.constellation != nil ? "星座:\(model.constellation!)" : "星座:-", UIColor.text.x1d1a1a, range: NSRange.init(location: 3, length: 3))
            jobLab.attributedText = mc_add(text: "职业: ", UIColor.text.x9b9b9b, font: PingFangSCRegularFont(12), appendText: model.position != nil ? "\(model.position!)" : "-", UIColor.text.x1d1a1a, appendFont: PingFangSCMediumFont(12))
                //mc_addForegroundColor(text: model.position != nil ? "职业:\(model.position!)" : "职业:-", UIColor.text.x1d1a1a, range: NSRange.init(location: 3, length: model.position?.length ?? 2))
            degreeLab.attributedText = mc_add(text: "学历: ", UIColor.text.x9b9b9b, font: PingFangSCRegularFont(12), appendText: model.degree != nil ? "\(model.degree!)" : "-", UIColor.text.x1d1a1a, appendFont: PingFangSCMediumFont(12))
                //mc_addForegroundColor(text: model.degree != nil ? "学历:\(model.degree!)" : "学历:-", UIColor.text.x1d1a1a, range: NSRange.init(location: 3, length: model.degree?.length ?? 2))
            signatureLab.attributedText = mc_add(text: "签名: ", UIColor.text.x9b9b9b, font: PingFangSCRegularFont(12), appendText: model.signature != nil ? "\(model.signature!)" : "-", UIColor.text.x1d1a1a, appendFont: PingFangSCMediumFont(12))
            hobbiesModelArr = model.hobbyList ?? []
            tagsModelArr = model.tagList ?? []
        }
    }
    
    var isSelf: Bool = false {
        didSet {
            editButton.isHidden = !isSelf
        }
    }
    
    lazy var editButton: UIButton = {
        let button = UIButton()
        button.setTitle("编辑", for: .normal)
        button.setTitleColor(UIColor(hex:0x1d1a1a), for: .normal)
        button.titleLabel?.font = PingFangSCMediumFont(12)
        button.addTarget(self, action: #selector(mineInfoController(didClickEditButton:)), for: .touchUpInside)
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 0)
        return button
    }()
    
    lazy var heightLab: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex:0x9b9b9b)
        return label
    }()
    
    lazy var weightLab: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex:0x9b9b9b)
        return label
    }()
    
    lazy var starLab: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex:0x9b9b9b)
        return label
    }()
    
    lazy var jobLab: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex:0x9b9b9b)
        return label
    }()
    
    lazy var degreeLab: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex:0x9b9b9b)
        return label
    }()
    
    lazy var signatureLab: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex:0x9b9b9b)
        return label
    }()
    
    lazy var hobbiesLab: UILabel = {
        let label = UILabel()
        label.font = PingFangSCSemiboldFont(16)
        label.textColor = UIColor(hex:0x1d1a1a)
        label.text = "兴趣爱好"
        return label
    }()
    
    lazy var hobbiesArr: [UILabel] = {
        return []
    }()
    
    lazy var hobbiesModelArr: [MTTagModel] = {
        return []
    }()
    
    lazy var hobbiesWidth: [CGFloat] = {
        return []
    }()
    
    lazy var tagsLab: UILabel = {
        let label = UILabel()
        label.font = PingFangSCSemiboldFont(16)
        label.textColor = UIColor(hex:0x1d1a1a)
        label.text = "标签"
        return label
    }()
    
    lazy var tagsArr: [UILabel] = {
        return []
    }()
    
    lazy var tagsModelArr: [MTTagModel] = {
        return []
    }()
    
    lazy var tagsWidth: [CGFloat] = {
        return []
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(hex: 0xf6f6f6)
        title = "个人信息"
//        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 56, height: 20))
//        button.setTitle("编辑", for: .normal)
//        button.setTitleColor(UIColor(hex:0x1d1a1a), for: .normal)
//        button.titleLabel?.font = PingFangSCMediumFont(12)
//        button.addTarget(self, action: #selector(mineInfoController(didClickEditButton:)), for: .touchUpInside)
        let rightBarButton = UIBarButtonItem(customView: editButton)
        editButton.snp.makeConstraints { (make) in
            make.width.equalTo(56)
            make.height.equalTo(20)
        }
        self.navigationItem.rightBarButtonItem = rightBarButton
        
        let basicView = basicInfoView()
        view.addSubview(basicView)
        basicView.snp.makeConstraints { (make) in
            make.left.equalTo(10)
            make.top.equalTo(20 + NavigationBarHeight + StatusBarHeight)
            make.right.equalTo(-10)
            make.height.equalTo(100)
        }
        
        let hobbView = hobbiesView()
        if hobbiesModelArr.count > 0 {
            view.addSubview(hobbView)
            hobbView.snp.makeConstraints { (make) in
                make.left.equalTo(10)
                make.top.equalTo(basicView.snp.bottom).offset(30)
                make.right.equalTo(-10)
            }
        }
        
        let top = hobbiesModelArr.count > 1 ? 30 : 70
        if tagsModelArr.count > 0 {
            let tagView = tagsView()
            view.addSubview(tagView)
            tagView.snp.makeConstraints { (make) in
                make.left.equalTo(10)
                make.top.equalTo(hobbiesModelArr.count > 0 ? hobbView.snp.bottom : basicView.snp.bottom).offset(hobbiesModelArr.count > 0 ? top : 30)
                make.right.equalTo(-10)
            }
        }
    }
}

extension MineInfoViewController {
    private func basicInfoView() -> UIView {
        let view  = UIView()
        view.backgroundColor = .clear
        view.addSubview(heightLab)
        view.addSubview(weightLab)
        view.addSubview(starLab)
        view.addSubview(jobLab)
        view.addSubview(degreeLab)
        view.addSubview(signatureLab)
        
        heightLab.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview()
        }
        
        weightLab.snp.makeConstraints { (make) in
            make.centerX.equalTo(view).offset(20)
            make.centerY.equalTo(heightLab)
        }
        
        starLab.snp.makeConstraints { (make) in
            make.left.equalTo(heightLab)
            make.top.equalTo(heightLab.snp.bottom).offset(10)
            
        }
        
        jobLab.snp.makeConstraints { (make) in
            make.left.equalTo(weightLab)
            make.centerY.equalTo(starLab)
        }
        
        degreeLab.snp.makeConstraints { (make) in
            make.left.equalTo(heightLab)
            make.top.equalTo(starLab.snp.bottom).offset(10)
            
        }
        
        signatureLab.snp.makeConstraints { (make) in
            make.left.equalTo(heightLab)
            make.top.equalTo(degreeLab.snp.bottom).offset(10)
            
        }
        
        return view
    }
    
    private func hobbiesView() -> UIView {
        let view  = UIView()
        view.backgroundColor = .clear
        view.addSubview(hobbiesLab)
        hobbiesLab.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
        }
        
        guard hobbiesModelArr.count != 0 else { return UIView()}
        if hobbiesModelArr.count > 1 {
            for i in 0 ..< hobbiesModelArr.count {
                let lab = UILabel()
                lab.backgroundColor = UIColor(hex:0xffc000)
                lab.text = hobbiesModelArr[i].name
                lab.textColor = UIColor(hex:0x1d1a1a)
                lab.font = PingFangSCMediumFont(12)
                lab.textAlignment = .center
                lab.layer.cornerRadius = 12
                lab.clipsToBounds = true
                view.addSubview(lab)
                hobbiesArr.append(lab)
                
                let labW = hobbiesModelArr[i].name!.size(withFont: (lab.font)!).width + 20
                hobbiesWidth.append(labW)
            }
            
            hobbiesArr.snp.distributeDetermineWidthViews(verticalSpacing: 10, horizontalSpacing: 10, maxWidth: ScreenWidth - 20, determineWidths: hobbiesWidth, itemHeight: 24, edgeInset: UIEdgeInsets(top: 10,left: 10, bottom: 0,right: 10), topConstrainView: hobbiesLab)
        } else {
            let lab = UILabel()
            lab.backgroundColor = UIColor(hex:0xffc000)
            lab.text = hobbiesModelArr[0].name
            lab.textColor = UIColor(hex:0x1d1a1a)
            lab.font = PingFangSCMediumFont(12)
            lab.textAlignment = .center
            lab.layer.cornerRadius = 12
            lab.clipsToBounds = true
            view.addSubview(lab)
            
            let labW = hobbiesModelArr[0].name!.size(withFont: (lab.font)!).width + 20
            lab.snp.makeConstraints { (make) in
                make.left.equalTo(10)
                make.top.equalTo(hobbiesLab.snp.bottom).offset(10)
                make.width.equalTo(labW)
                make.height.equalTo(24)
            }
        }
        
        return view
    }
    
    private func tagsView() -> UIView {
        let view  = UIView()
        view.backgroundColor = .clear
        view.addSubview(tagsLab)
        tagsLab.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
        }
        
        if tagsModelArr.count > 1 {
            for i in 0 ..< tagsModelArr.count {
                let lab = UILabel()
                lab.backgroundColor = UIColor(hex:0xffc000)
                lab.text = tagsModelArr[i].name
                lab.textColor = UIColor(hex:0x1d1a1a)
                lab.font = PingFangSCMediumFont(12)
                lab.textAlignment = .center
                lab.layer.cornerRadius = 12
                lab.clipsToBounds = true
                view.addSubview(lab)
                tagsArr.append(lab)
                
                let labW = tagsModelArr[i].name!.size(withFont: (lab.font)!).width + 20
                tagsWidth.append(labW)
            }
            
            tagsArr.snp.distributeDetermineWidthViews(verticalSpacing: 10, horizontalSpacing: 10, maxWidth: ScreenWidth - 20, determineWidths: tagsWidth, itemHeight: 24, edgeInset: UIEdgeInsets(top: 10,left: 10, bottom: 10,right: 10), topConstrainView: tagsLab)
        } else {
            let lab = UILabel()
            lab.backgroundColor = UIColor(hex:0xffc000)
            lab.text = tagsModelArr[0].name
            lab.textColor = UIColor(hex:0x1d1a1a)
            lab.font = PingFangSCMediumFont(12)
            lab.textAlignment = .center
            lab.layer.cornerRadius = 12
            lab.clipsToBounds = true
            view.addSubview(lab)
            
            let labW = tagsModelArr[0].name!.size(withFont: (lab.font)!).width + 20
            
            lab.snp.makeConstraints { (make) in
                make.left.equalTo(10)
                make.top.equalTo(tagsLab.snp.bottom).offset(10)
                make.width.equalTo(labW)
                make.height.equalTo(24)
            }
        }
        
        return view
    }
}

// MARK: ---------------------------------------- 事件方法 --------------------------------------------
@objc
extension MineInfoViewController {
    
    private func mineInfoController(didClickEditButton sender: UIButton) {
        let c = MineProfileController()
        c.model = model
        self.navigationController?.pushViewController(c, animated: true)
    }
}

extension MineInfoViewController {
    
    /// 设置字体颜色
    public func mc_add(text: String,_ color: UIColor, font: UIFont = PingFangSCSemiboldFont(12), appendText: String = "-", _ appendColor: UIColor, appendFont: UIFont = PingFangSCMediumFont(12)) -> NSMutableAttributedString {
        
        let markAttrs = [NSAttributedString.Key.foregroundColor: color, NSAttributedString.Key.font : font]
        let appendAttrs = [NSAttributedString.Key.foregroundColor: appendColor, NSAttributedString.Key.font : appendFont]
        
        let markAttribute = NSMutableAttributedString(string: text, attributes: markAttrs)
        let markAppendAttr = NSAttributedString(string: appendText, attributes: appendAttrs)
        
        markAttribute.append(markAppendAttr)
        
        return markAttribute
    }
    
    /// 设置字体大小
    public func mc_addFont(text: String,_ font: UIFont, on range: NSRange) -> NSMutableAttributedString {
        let markAttribute = NSMutableAttributedString(string: text)
        
        if markAttribute.length < range.location + range.length { return markAttribute }
        
        let attrs = [NSAttributedString.Key.font: font]
        markAttribute.addAttributes(attrs, range: range)
        return markAttribute
    }
    
    /// 设置字体颜色
    public func mc_addForegroundColor(text: String,_ color: UIColor, range: NSRange) -> NSMutableAttributedString {
        let markAttribute = NSMutableAttributedString(string: text)
        if markAttribute.length < range.location + range.length {
            return markAttribute
        }
        
        let attrs = [NSAttributedString.Key.foregroundColor: color]
        markAttribute.addAttributes(attrs, range: range)
        return markAttribute
    }
    
    
}
